/*
 * File:   main.c
 * Author: rslmanu
 *
 * Created on 11 mai 2009, 01:26
 */

/*
Copyright (C) 2009 Roussel Emmanuel

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "fctions.h"

#define UPD_TIME 60000 //en us
#define LENGTH 5    //nbre de bouts de serpent au départ.


int main(int argc, char** argv) {
    struct termios prev;

    int loop_ok = 1;
    char c;
    int dir=EST;
    int i;

    bout *serpent = NULL;   //LE SERPENT !!!
    pomme pom;      //LA POMME !!
    
    for(i=0; i<LENGTH+1; i++){    //crée le serpent de départ
        ll_push(&serpent, OFFSET_X+i+1, OFFSET_Y + TAILLE_Y/2);
    }

    if (reconfigure_terminal(&prev) == -1) return 1;
    srand(time(NULL));

    REINIT_COULEUR();
    CLS();
    TITRE_CONSOLE("~~~~>   Serpent !   <~~~~");

    print_map();

    // bcle ppale
    while (loop_ok) {
        //----- gestion des mvts du joueur
        c = read_char();
        //if (c < 0) continue;
        switch (c) {
            case '\n': //stop si appui sur entrée        //0x1B: //stop si appui sur echap
                loop_ok = 0;
                continue;

            case 'o':
                if(dir != SUD)  //pr éviter quon puisse faire marche arrière.
                    dir=NORD;
                break;
            case 'l':
                if(dir != NORD)
                    dir=SUD;
                break;
            case 'k':
                if(dir != EST)
                    dir=OUEST;
                break;
            case 'm':
                if(dir != OUEST)
                    dir=EST;
                break;
                //default:
                //	printf("[%02X] ", c);
                //	fflush(stdout);
        }
        
        //----- gestion du serpent
        update_serpent(&serpent, dir);
        update_pomme(&serpent, &pom);
        if(check_perdu(serpent)){
            CURSEUR(OFFSET_X + 2, OFFSET_Y + 1);
            printf("  DAMNED ! Le serpent est mort !  ");
            CURSEUR(OFFSET_X + 5, OFFSET_Y + 3);
            printf("  score : %d  ", (ll_length(serpent)-1-LENGTH)*10);
            UPD();
            usleep(2000000);
            while(read_char() < 0);//attend l'appui sur une touche
            loop_ok = 0;
            continue;
        }

        if(dir == NORD || dir == SUD)
            usleep(UPD_TIME*1.4);   //petit effet
        else
            usleep(UPD_TIME);

    }

    //restaure la console
    if (restaure_terminal(&prev) == -1) return 1;
    CLS();
    printf("score : %d\n\n", (ll_length(serpent)-1-LENGTH)*10);
    ll_clear(&serpent); //le serpent est mort. Paix à son âme.
    
    return 0;
}




